package nameless.aistudy.common.result;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

import nameless.aistudy.common.enumeration.message.MessageEnum;

@Data
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    // 状态码
    private int code;

    // 返回消息
    private String message;

    // 返回数据
    private T data;

    // 时间戳
    private long timestamp = System.currentTimeMillis();

    /* 成功响应结果 工具方法 */

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return success(HttpStatus.OK.getReasonPhrase(), data);
    }

    public static <T> Result<T> success(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(HttpStatus.OK.value());
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    /* 失败响应结果 工具方法 */

    public static <T> Result<T> fail() {
        return fail(HttpStatus.BAD_REQUEST);
    }

    public static <T> Result<T> fail(String message) {
        return fail(HttpStatus.BAD_REQUEST, message);
    }

    public static <T> Result<T> fail(HttpStatus status) {
        return fail(status, status.getReasonPhrase());
    }

    public static <T> Result<T> fail(HttpStatus status, String message) {
        Result<T> result = new Result<>();
        result.setCode(status.value());
        result.setMessage(message);
        return result;
    }

    /* 其他常见业务异常 */

    // 404 Not Found (资源不存在)
    public static <T> Result<T> notFound() {
        return fail(HttpStatus.NOT_FOUND, MessageEnum.NOT_FOUND.getMessage());
    }

    // 404 Not Found (资源不存在) (自定义响应消息)
    public static <T> Result<T> notFound(String message) {
        return fail(HttpStatus.NOT_FOUND, message);
    }

    // 401 Unauthorized (未认证)
    public static <T> Result<T> unauthorized() {
        return fail(HttpStatus.UNAUTHORIZED, MessageEnum.UNAUTHORIZED.getMessage());
    }

    // 403 Forbidden (无访问权限)
    public static <T> Result<T> forbidden() {
        return fail(HttpStatus.FORBIDDEN, MessageEnum.FORBIDDEN.getMessage());
    }

}
